const {service} = require("../request");
const model = require("../../utils/modelUtil");

/**
 * 门派
 */
class School {
    constructor(user) {
        this.user = user;
    }


    /**
     * 获取任务数据
     * @param schoolId 门派ID
     * @returns {Promise<unknown>}
     */
    getTaskData() {
        return new Promise((resolve, reject) => {
            service({
                method: 'POST',
                url: this.user.game.reqUrl + '/getSchoolTask',
                data: model.userModel('getSchoolTask', {schoolId: this.user.game.other.baseValueAy[23]}, this.user.game.isWeb, this.user.game.data),
            }).then(res => {
                const resData = res.data;
                resolve(resData.code===200?resData.MsgData:{});
            }).catch(err => {
                reject(err);
            })
        })
    }

    /**
     * 接取任务
     * @param index 任务索引
     * @returns {Promise<unknown>}
     */
    acceptTask(index) {
        return new Promise((resolve, reject) => {
            service({
                method: 'POST',
                url: this.user.game.reqUrl + '/acceptSchoolTask',
                data: model.userModel('acceptSchoolTask', {schoolId: this.user.game.other.baseValueAy[23], index: index}, this.user.game.isWeb, this.user.game.data),
            }).then(res => {
                const resData = res.data;
                resolve(resData.code===200?resData.MsgData:{});
            }).catch(err => {
                reject(err);
            })
        })
    }

    /**
     * 完成任务
     * @param index 任务索引
     * @param itemId 物品ID
     * @returns {Promise<unknown>}
     */
    completeTask(index, itemId = 0) {
        return new Promise((resolve, reject) => {
            service({
                method: 'POST',
                url: this.user.game.reqUrl + '/schoolTaskComplete',
                data: model.userModel('schoolTaskComplete', {schoolId: this.user.game.other.baseValueAy[23], index: index, itemId: itemId}, this.user.game.isWeb, this.user.game.data),
            }).then(res => {
                const resData = res.data;
                resolve(resData.code===200?resData.MsgData:{});
            }).catch(err => {
                reject(err);
            })
        })
    }
}

module.exports = School;